package com.itfenghuang.day13;

import java.util.concurrent.Callable;
import java.util.concurrent.locks.ReentrantLock;

public class A01_复习昨天的内容 {
    public static void main(String[] args) {

        Cc cc = new Cc();
        Thread thread1 = new Thread(cc,"窗口1");
        Thread thread2 = new Thread(cc,"窗口2");
        Thread thread3 = new Thread(cc,"窗口3");
        thread1.start();
        thread2.start();
        thread3.start();


    }
}
class Cc implements Runnable {
    private int ticket=100;
    private Object obj=new Object();//同步代码块
    public ReentrantLock lock=new ReentrantLock();

//    @Override
//    public void run() {
//        while (true){
//            synchronized (obj){
//                try {
//                    Thread.sleep(100);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                if (ticket>0){
//                    System.out.println(Thread.currentThread().getName() +" 售出: " + ticket + "号");
//                    ticket--;
//                }else {
//                    break;
//                }
//            }
//        }
//    }

//    @Override
//    public void run() {
//            while (true){
//                try {
//                    Thread.sleep(150);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                method();
//                if (ticket<=0){
//                    break;
//                }
//            }
//    }
//    private void method() {
//        if (ticket>0){
//            System.out.println(Thread.currentThread().getName() + " 售出 " + ticket + "号");
//            ticket--;
//        }
//    }


    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.lock();
            if (ticket>0){
                System.out.println(Thread.currentThread().getName() + " 售出 " + ticket + " 张");
                ticket--;
            }
            lock.unlock();
            if (ticket<=0){
                break;
            }
        }
    }
}